package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbEquipmentBo;
import com.ruoyi.tb.lmt.domain.vo.TbEquipmentVo;
import com.ruoyi.tb.lmt.domain.TbEquipment;
import com.ruoyi.tb.lmt.mapper.TbEquipmentMapper;
import com.ruoyi.tb.lmt.service.ITbEquipmentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 总成部件Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-30
 */
@RequiredArgsConstructor
@Service
public class TbEquipmentServiceImpl implements ITbEquipmentService {

    private final TbEquipmentMapper baseMapper;

    /**
     * 查询总成部件
     */
    @Override
    public TbEquipmentVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询总成部件列表
     */
    @Override
    public TableDataInfo<TbEquipmentVo> queryPageList(TbEquipmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbEquipment> lqw = buildQueryWrapper(bo);
        Page<TbEquipmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询总成部件列表
     */
    @Override
    public List<TbEquipmentVo> queryList(TbEquipmentBo bo) {
        LambdaQueryWrapper<TbEquipment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbEquipment> buildQueryWrapper(TbEquipmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbEquipment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLmtType()), TbEquipment::getLmtType, bo.getLmtType());
        lqw.like(StringUtils.isNotBlank(bo.getCategoryCode()), TbEquipment::getCategoryCode, bo.getCategoryCode());
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentName()), TbEquipment::getEquipmentName, bo.getEquipmentName());
        return lqw;
    }

    /**
     * 新增总成部件
     */
    @Override
    public Boolean insertByBo(TbEquipmentBo bo) {
        TbEquipment add = BeanUtil.toBean(bo, TbEquipment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改总成部件
     */
    @Override
    public Boolean updateByBo(TbEquipmentBo bo) {
        TbEquipment update = BeanUtil.toBean(bo, TbEquipment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbEquipment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除总成部件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
